using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectDoLuuNiem()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DOLUUNIEM
			};
			return View(CreateViewModel(data));
		}

        public ActionResult SelectDoLuuNiemPaging(int page = 1)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_DOLUUNIEM,
                CurrentPage = page
            };
            return PartialView("Templates/TH_ListDoLuuNiem", CreateViewModel(data));
        }

        [AcceptVerbs(HttpVerbs.Delete)]
        public JsonResult DeleteDoLuuNiem(int id)
        {
            // Delete do luu niem truoc
            var result = _repDoLuuNiem.Delete(id);
            if (result)
            {
                var spItem = _repSanPham.SelectByID(id);
                var fileName = spItem.HinhMinhHoa;
                result = _repSanPham.Delete(id);
                if (result)
                {
                    if (fileName != "noimages.jpg")
                    {
                        var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
                        DeleteFile(fullPath);
                    }
                }
            }

            return Json(new
            {
                Success = result,
                RecordCount = _repDoLuuNiem.SelectAll().Count,
                DeleteId = id
            });
        }

		public ActionResult DetailOfDoLuuNiem(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DOLUUNIEM,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateDoLuuNiem(FormCollection forms)
		{
            string refName = string.Empty;
            long gia;
            int soluongton;

			string id = forms.GetValues("DoLuuNiem_ID")[0];
			string xuatxu = forms.GetValues("DoLuuNiem_XuatXu")[0];
			string nhasanxuat = forms.GetValues("DoLuuNiem_NhaSanXuat")[0];
			string trongluong = forms.GetValues("DoLuuNiem_TrongLuong")[0];
            string tensanpham = forms.GetValues("DoLuuNiem_TenSanPham")[0];
            string mota = forms.GetValues("DoLuuNiem_NoiDung")[0];
            var file = Request.Files["DoLuuNiem_HinhMinhHoa"];

            // Update do luu niem
            var doluuniem = _repDoLuuNiem.SelectByID(int.Parse(id));
            
			doluuniem.XuatXu = xuatxu;
			doluuniem.NhaSanXuat = nhasanxuat;
			doluuniem.TrongLuong = trongluong;
            doluuniem.MoTa = mota;

			var result = _repDoLuuNiem.Save();

            // Update san pham
            if (result)
            {
                var spItem = _repSanPham.SelectByID(int.Parse(id));

                if (int.TryParse(forms.GetValues("DoLuuNiem_SoLuongTon")[0].Replace(",", string.Empty).Replace(".", string.Empty), out soluongton))
                {
                    spItem.SoLuongTon = soluongton;
                }

                if (long.TryParse(forms.GetValues("DoLuuNiem_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
                {
                    spItem.GiaBan = gia;
                }

                spItem.TenSanPham = tensanpham;

                if (file.ContentLength != 0)
                {
                    result = UploadFile(file, "/Content/Images/Items/", ref refName);
                    spItem.HinhMinhHoa = refName;
                }

                result = _repSanPham.Save();
            }

			var data = new DataTransferViewModel
			{
                EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_DOLUUNIEM,
                IntID = int.Parse(id.Replace(".", string.Empty).Replace(",", string.Empty)),
                InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
            return View("DetailOfDoLuuNiem", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertDoLuuNiem(FormCollection forms)
		{
            long gia;
            int soluongton;

			string xuatxu = forms.GetValues("DoLuuNiem_XuatXu")[0];
			string nhasanxuat = forms.GetValues("DoLuuNiem_NhaSanXuat")[0];
			string trongluong = forms.GetValues("DoLuuNiem_TrongLuong")[0];
            string tensanpham = forms.GetValues("DoLuuNiem_TenSanPham")[0];
            string mota = forms.GetValues("DoLuuNiem_NoiDung")[0];
            var file = Request.Files["DoLuuNiem_HinhMinhHoa"];

            string refName = string.Empty;
            var result = false;

            // Insert san pham truoc
            var spItem = new SanPham();

            if (int.TryParse(forms.GetValues("DoLuuNiem_SoLuongTon")[0].Replace(",", string.Empty).Replace(".", string.Empty), out soluongton))
            {
                spItem.SoLuongTon = soluongton;
            }

            if (long.TryParse(forms.GetValues("DoLuuNiem_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
            {
                spItem.GiaBan = gia;
            }

            spItem.LoaiSanPham = StringEnum.GetStringValue(LoaiSanPham.DO_LUU_NIEM);
            spItem.TenSanPham = tensanpham;

            if (file.ContentLength != 0)
            {
                if(UploadFile(file, "/Content/Images/Items/", ref refName))
                {
                    spItem.HinhMinhHoa = refName;
                }
            }
			
			result = _repSanPham.Insert(spItem);

            // Neu insert san pham thanh cong, thi moi insert tiep sach
            if (result)
            {
                var newItem = new DoLuuNiem
                {
                    ID = spItem.ID,
                    XuatXu = xuatxu,
                    NhaSanXuat = nhasanxuat,
                    TrongLuong = trongluong,
                    MoTa = mota
                };
                result = _repDoLuuNiem.Insert(newItem);
            }
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DOLUUNIEM,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectDoLuuNiem", CreateViewModel(data));
		}
	}
}